package com.tuanzi.loan.business.repository.family.impl;

import com.tuanzi.loan.business.repository.family.custom.StatLargePageDetailRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.family.largeamount.LoanStatLargePageDetailRequest;
import com.tuanzi.loan.vo.response.family.largeamount.LoanStatLargePageDetailResponse;
import org.apache.commons.lang3.StringUtils;
import org.jooq.*;
import org.jooq.impl.DSL;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.Date;

import static com.tuanzi.loan.generated.jooq.loan.Tables.LOAN_WHOLESALE_COMMENT;
import static com.tuanzi.loan.generated.jooq.stat.Tables.STAT_LARGE_PAGE_DETAIL;

/**
 * User: liangZongXi
 * Create time: 14:37 2019/5/27
 * Description:
 */
@Repository
public class StatLargePageDetailRepositoryImpl extends JOOQBaseRepositoryPlus implements StatLargePageDetailRepositoryCustom {

    @Override
    public Page<LoanStatLargePageDetailResponse> list(LoanStatLargePageDetailRequest searchRequest, Pageable pageable) {
        fields(STAT_LARGE_PAGE_DETAIL.fields());
        fields(LOAN_WHOLESALE_COMMENT.OWNERNAME);

        leftJoin(LOAN_WHOLESALE_COMMENT, STAT_LARGE_PAGE_DETAIL.PRODUCT_ID.eq(LOAN_WHOLESALE_COMMENT.TYPE));

        ge(STAT_LARGE_PAGE_DETAIL.STAT_DATE, searchRequest.getStartDate());
        le(STAT_LARGE_PAGE_DETAIL.STAT_DATE, searchRequest.getEndDate());

        eq(STAT_LARGE_PAGE_DETAIL.C_FROM, searchRequest.getFrom());
        eq(STAT_LARGE_PAGE_DETAIL.PRODUCT_ID, searchRequest.getProductId());

        orderBy(STAT_LARGE_PAGE_DETAIL.STAT_DATE, Sort.Direction.DESC);

        return search(pageable, record -> {
            LoanStatLargePageDetailResponse response = new LoanStatLargePageDetailResponse();
            response.setId(record.get(STAT_LARGE_PAGE_DETAIL.ID));
            response.setStatDate(record.get(STAT_LARGE_PAGE_DETAIL.STAT_DATE));
            response.setProductName(record.get(LOAN_WHOLESALE_COMMENT.OWNERNAME));
            response.setFrom(record.get(STAT_LARGE_PAGE_DETAIL.C_FROM));
            response.setCanApply(record.get(STAT_LARGE_PAGE_DETAIL.CAN_APPLY));
            response.setToSecond(record.get(STAT_LARGE_PAGE_DETAIL.ALL_FAIL));
            response.setApplyCount(record.get(STAT_LARGE_PAGE_DETAIL.APPLY_COUNT));
            response.setApplySuccess(record.get(STAT_LARGE_PAGE_DETAIL.APPLY_SUCCESS));
            response.setApplyFail(record.get(STAT_LARGE_PAGE_DETAIL.APPLY_FAIL));
            return response;
        });
    }

    @Override
    public Page<LoanStatLargePageDetailResponse> listChannelNdIn(LoanStatLargePageDetailRequest searchRequest, Pageable pageable, String[] channelNds) {

        Condition condition = DSL.trueCondition();
        Date startDate = searchRequest.getStartDate();
        if (null != startDate) {
            condition = condition.and(STAT_LARGE_PAGE_DETAIL.STAT_DATE.ge(new java.sql.Date(startDate.getTime())));
        }
        Date endDate = searchRequest.getEndDate();
        if (null != endDate) {
            condition = condition.and(STAT_LARGE_PAGE_DETAIL.STAT_DATE.le(new java.sql.Date(endDate.getTime())));
        }
        String from = searchRequest.getFrom();
        if (StringUtils.isNotBlank(from)) {
            condition = condition.and(STAT_LARGE_PAGE_DETAIL.C_FROM.eq(from));
        } else {
            condition = condition.and(STAT_LARGE_PAGE_DETAIL.C_FROM.in(channelNds));
        }

        Table<Record4<java.sql.Date, String, String, String>> record4Table = dslContext.select(STAT_LARGE_PAGE_DETAIL.STAT_DATE, STAT_LARGE_PAGE_DETAIL.C_FROM,
                STAT_LARGE_PAGE_DETAIL.CAN_APPLY, STAT_LARGE_PAGE_DETAIL.ALL_FAIL)
                .from(STAT_LARGE_PAGE_DETAIL).where(condition)
                .groupBy(STAT_LARGE_PAGE_DETAIL.STAT_DATE, STAT_LARGE_PAGE_DETAIL.C_FROM).asTable();

        Table<Record5<java.sql.Date, String, BigDecimal, BigDecimal, BigDecimal>> record5Table = dslContext.select(STAT_LARGE_PAGE_DETAIL.STAT_DATE, STAT_LARGE_PAGE_DETAIL.C_FROM,
                DSL.sum(STAT_LARGE_PAGE_DETAIL.APPLY_COUNT.cast(Integer.class)).as("APPLY_COUNT"),
                DSL.sum(STAT_LARGE_PAGE_DETAIL.APPLY_SUCCESS.cast(Integer.class)).as("APPLY_SUCCESS"),
                DSL.sum(STAT_LARGE_PAGE_DETAIL.APPLY_FAIL.cast(Integer.class)).as("APPLY_FAIL"))
                .from(STAT_LARGE_PAGE_DETAIL).where(condition)
                .groupBy(STAT_LARGE_PAGE_DETAIL.STAT_DATE, STAT_LARGE_PAGE_DETAIL.C_FROM).asTable();

        SelectQuery<Record7<java.sql.Date, String, String, String, Integer, Integer, Integer>> query = dslContext.select(
                record4Table.field(STAT_LARGE_PAGE_DETAIL.STAT_DATE),
                record4Table.field(STAT_LARGE_PAGE_DETAIL.C_FROM),
                record4Table.field(STAT_LARGE_PAGE_DETAIL.CAN_APPLY),
                record4Table.field(STAT_LARGE_PAGE_DETAIL.ALL_FAIL),
                record5Table.field("APPLY_COUNT", Integer.class),
                record5Table.field("APPLY_SUCCESS", Integer.class),
                record5Table.field("APPLY_FAIL", Integer.class)
        ).from(
                record4Table.leftJoin(record5Table)
                        .on(
                                record4Table.field(STAT_LARGE_PAGE_DETAIL.STAT_DATE).eq(record5Table.field(STAT_LARGE_PAGE_DETAIL.STAT_DATE))
                                        .and(record4Table.field(STAT_LARGE_PAGE_DETAIL.C_FROM).eq(record5Table.field(STAT_LARGE_PAGE_DETAIL.C_FROM)))
                        )
        ).orderBy(record4Table.field(STAT_LARGE_PAGE_DETAIL.STAT_DATE).desc()).getQuery();


        return search(query, pageable, record -> {
            LoanStatLargePageDetailResponse response = new LoanStatLargePageDetailResponse();
            response.setStatDate(record.get(STAT_LARGE_PAGE_DETAIL.STAT_DATE));
            response.setFrom(record.get(STAT_LARGE_PAGE_DETAIL.C_FROM));
            response.setCanApply(record.get(STAT_LARGE_PAGE_DETAIL.CAN_APPLY));
            response.setToSecond(record.get(STAT_LARGE_PAGE_DETAIL.ALL_FAIL));
            Object applyCount = record.get("APPLY_COUNT");
            response.setApplyCount(applyCount == null ? null : applyCount.toString());
            Object applySuccess = record.get("APPLY_SUCCESS");
            response.setApplySuccess(applySuccess == null ? null : applySuccess.toString());
            Object applyFail = record.get("APPLY_FAIL");
            response.setApplyFail(applyFail == null ? null : applyFail.toString());
            return response;
        });
    }

    @Override
    protected TableImpl getTable() {
        return STAT_LARGE_PAGE_DETAIL;
    }
}
